﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Bio.Data.Interfaces.Drawing;

namespace Bio.Data.Providers.Structure.XRNA.Internal
{
    internal class XRNANucleotideShape
    {
        public static XRNANucleotideShape TestCreateDefaultText(string xrnaLine)
        {
            if (xrnaLine != null)
            {
                if (Regex.IsMatch(xrnaLine, NTTextRenderingDefinition))
                {
                    string[] tokens = Regex.Split(xrnaLine, " ");
                    Debug.Assert(tokens.Length == 4);
                    Debug.Assert(!string.IsNullOrEmpty(tokens[0]));
                    int typefaceID;
                    double fontSize;
                    if (Int32.TryParse(tokens[2], out typefaceID) &&
                        Double.TryParse(tokens[1], out fontSize))
                    {
                        //string color = "#" + tokens[3];
                        //XRNATextShape shape = XRNATextShape.Create(typefaceID, fontSize, color.PadRight(7, '0'));
                        string color = "#" + tokens[3].PadLeft(6, '0');
                        XRNATextShape shape = XRNATextShape.Create(typefaceID, fontSize, color);
                        string[] ranges = Regex.Split(Regex.Split(tokens[0], "#")[0], ",");
                        IEnumerable<int> assignedTo = from range in ranges
                                                      from idx in XRNAFile.ExpandXRNARange(range, true)
                                                      select idx - 1;
                        return new XRNANucleotideShape(shape, assignedTo, NucleotideMetadata.RenderingMode.Text);
                    }
                }
            }
            return null; //Some error was encountered or this was not a valid line. In the future, we would probably throw an error to distinguish
        }

        public static XRNANucleotideShape TestCreateTextSubstitution(string xrnaLine)
        {
            if (xrnaLine != null)
            {
                if (Regex.IsMatch(xrnaLine, NTTextSubstitution))
                {
                    string[] tokens = Regex.Split(xrnaLine, " ");
                    Debug.Assert(tokens.Length == 5);
                    Debug.Assert(!string.IsNullOrEmpty(tokens[0]));
                    int typefaceID;
                    double fontSize;
                    if (double.TryParse(tokens[1], out fontSize) &&
                        Int32.TryParse(tokens[2], out typefaceID))
                    {
                        string color = "#" + tokens[4].PadLeft(6, '0');
                        XRNATextShape shape = XRNATextShape.Create(typefaceID, fontSize, color);
                        string[] ranges = Regex.Split(Regex.Split(tokens[0], "#")[0], ",");
                        IEnumerable<int> assignedTo = from range in ranges
                                                      from idx in XRNAFile.ExpandXRNARange(range, true)
                                                      select idx - 1;
                        return new XRNANucleotideShape(shape, assignedTo, tokens[3][0]);
                    }
                }
            }
            return null;
        }

        public static XRNANucleotideShape TestCreateSymbolSubstitution(string xrnaLine)
        {
            if (xrnaLine != null)
            {
                if (Regex.IsMatch(xrnaLine, NTSymbolSubstitution))
                {
                    string[] tokens = Regex.Split(xrnaLine, " ");
                    Debug.Assert(tokens.Length == 4);
                    Debug.Assert(!string.IsNullOrEmpty(tokens[0]));
                    double fontSize;
                    if(double.TryParse(tokens[1], out fontSize))
                    {
                        string color = "#" + tokens[3].PadLeft(6, '0');
                        XRNATextShape shape = XRNATextShape.Create(13, fontSize, color);
                        string[] ranges = Regex.Split(Regex.Split(tokens[0], "#")[0], ",");
                        IEnumerable<int> assignedTo = from range in ranges
                                                      from idx in XRNAFile.ExpandXRNARange(range, true)
                                                      select idx - 1;
                        return new XRNANucleotideShape(shape, assignedTo, tokens[2][0]);
                    }
                }
            }
            return null;
        }

        public static XRNANucleotideShape TestCreateCircleSubstitution(string xrnaLine)
        {
            if (xrnaLine != null)
            {
                if (Regex.IsMatch(xrnaLine, NTCircleSubstitution))
                {
                    string[] tokens = Regex.Split(xrnaLine, "#");
                    Debug.Assert(tokens.Length == 2);
                    Debug.Assert(!string.IsNullOrEmpty(tokens[0]));
                    XRNACircleShape shape = XRNACircleShape.Create(tokens[1]);
                    string[] ranges = Regex.Split(Regex.Split(tokens[0], "#")[0], ",");
                    IEnumerable<int> assignedTo = from range in ranges
                                                  from idx in XRNAFile.ExpandXRNARange(range, true)
                                                  select idx - 1;
                    return new XRNANucleotideShape(shape, assignedTo, NucleotideMetadata.RenderingMode.Circle);
                }
            }
            return null;
        }

        public IEnumerable<int> AssignedTo
        {
            get { return _assignedTo; }
            private set { _assignedTo = value; }
        }

        public IShape Visual
        {
            get { return _visual; }
            private set { _visual = value; }
        }

        public NucleotideMetadata.RenderingMode RenderingStyle
        {
            get { return _renderingStyle; }
            private set { _renderingStyle = value; }
        }

        public bool IsTextSubstitution
        {
            get { return _isTextSubstitution; }
            private set { _isTextSubstitution = value; }
        }

        public char TextSubstitution
        {
            get { return _textSubstitution; }
            private set { _textSubstitution = value; }
        }

        #region Private Members and Methods

        private XRNANucleotideShape(ITextShape visual, IEnumerable<int> assignedTo, char textSubstitution)
        {
            RenderingStyle = NucleotideMetadata.RenderingMode.Text;
            IsTextSubstitution = true;
            AssignedTo = assignedTo;
            Visual = visual;
            TextSubstitution = textSubstitution;
        }

        private XRNANucleotideShape(IShape visual, IEnumerable<int> assignedTo, NucleotideMetadata.RenderingMode renderingStyle)
        {
            AssignedTo = assignedTo;
            Visual = visual;
            RenderingStyle = renderingStyle;
        }

        private IEnumerable<int> _assignedTo;
        private IShape _visual;
        private NucleotideMetadata.RenderingMode _renderingStyle;
        private bool _isTextSubstitution = false;
        private char _textSubstitution;
        private static string NTTextRenderingDefinition = @"[\d+(-?\d+),?]+#!s\s\d+[.\d0]*\s\d+\s[a-fA-F0-9]+";
        private static string NTTextSubstitution = @"[\d+(-?\d+),?]+#!C\s\d+[.\d0]*\s\d+\s[a-zA-Z]\s[a-fA-F0-9]+";
        private static string NTSymbolSubstitution = @"[\d+(-?\d+),?]+#!S\s\d+[.\d0]*\s[a-zA-Z]\s[a-fA-F0-9]+";
        private static string NTCircleSubstitution = @"[\d+(-?\d+),?]+#!c\s\d+.\d{2}\s\d+.\d{2}\s\d+.\d{2}\s\d+.\d{2}\s\d\s[a-zA-Z0-9]+";

        #endregion
    }
}
